using Avanzis.QueryFramework;
using NUnit.Framework;

namespace Avanzis.QueryBuilder.NUnit.MySql
{
    public class MySqlInsertBuilderTest : InsertBuilderTest
    {
        private Dialects previousDialect = Dialects.MSSql2005Dialect;

        [TestFixtureSetUp]
        public void Setup()
        {
            previousDialect = QueryFrameworkConfiguration.Dialect;
            QueryFrameworkConfiguration.Dialect = Dialects.MySql;
            QueryBuilder.QueryProcessor.ResetProcessor();
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            QueryFrameworkConfiguration.Dialect = previousDialect;

        }

        [Test]
        public override void InsertWithParameters()
        {
            InsertBuilder sb = new InsertBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "@FirstName");
            sb.AddFieldValue("LastName", "@LastName");

            string result = "INSERT INTO `People` (`FirstName`, `LastName`) VALUES (@FirstName, @LastName)";

            Assert.AreEqual(result, sb.ToString());
        }

        [Test]
        public override void InsertWithParametersAndValues()
        {
            InsertBuilder sb = new InsertBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "@FirstName");
            sb.AddFieldValue("LastName", "John Doe");

            string result = "INSERT INTO `People` (`FirstName`, `LastName`) VALUES (@FirstName, 'John Doe')";

            Assert.AreEqual(result, sb.ToString());
        }

        [Test]
        public override void SimpleInsertTest()
        {
            InsertBuilder sb = new InsertBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "John");
            sb.AddFieldValue("LastName", "Smith");

            string result = "INSERT INTO `People` (`FirstName`, `LastName`) VALUES ('John', 'Smith')";

            Assert.AreEqual(result, sb.ToString());

            InsertBuilder cloned = (InsertBuilder)sb.Clone();
            Assert.AreEqual(cloned.ToString(), sb.ToString());
        }

        [Test]
        public override void InsertWithStrangeCharacters()
        {
            InsertBuilder sb = new InsertBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "John Doe");
            sb.AddFieldValue("LastName", "T.T");

            string result = "INSERT INTO `People` (`FirstName`, `LastName`) VALUES ('John Doe', 'T.T')";

            Assert.AreEqual(result, sb.ToString());
        }

        [Test]
        public override void InsertWithNullValue()
        {
            InsertBuilder sb = new InsertBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "John Doe");
            sb.AddFieldValue("LastName", null);

            string expectedResult = "INSERT INTO `People` (`FirstName`, `LastName`) VALUES ('John Doe', NULL)";

            string result = sb.ToString();

            Assert.AreEqual(expectedResult, result);
        }

    }
}
